<?php
use \Tuanduimao\Loader\App as App;
use \Tuanduimao\Utils as Utils;
use \Tuanduimao\Tuan as Tuan;
use \Tuanduimao\Excp as Excp;
use \Tuanduimao\Conf as Conf;


class TicketController extends \Tuanduimao\Loader\Controller {
	
	function __construct() {
	}


	function index() {
		Utils::out([
			"server"=>"Ticket Server On", 
			"group"=>$_SESSION['_group'],
			"user"=>$_SESSION['_user'],
			"isadmin"=>$_SESSION['_isadmin'],
			"_GET"=>$_GET,
			"_POST"=>$_POST
		]);
	}

	/**
	 * 查询余票
	 * @return [type] [description]
	 */
	function available() {

		// 只有登录用户可以查询
		if ( $_SESSION['_group'] != 'member' ) {
			throw new Excp("没有查询权限", 500, ['group'=>$_SESSION['_group'], 'should be'=>'member']);
		}

		$data = $_POST;
		$data['train_date'] = !empty($data['train_date']) ? $data['train_date'] : date("Y-m-d");
		$data['from_station'] = !empty($data['from_station']) ? $data['from_station'] :'BJP';
		$data['to_station'] = !empty($data['to_station']) ? $data['to_station'] :'SHH';

		// 聚合数据申请
		// @see https://www.juhe.cn/docs/api/id/173/aid/580
		$data['key'] = '去聚合数据申请';
		$api = "http://op.juhe.cn/trainTickets/ticketsAvailable";
		
		$resp = Utils::Request('POST', $api, ["data"=>$data]);

		Utils::out($resp);

		// echo json_decode($resp);
	}

}